const path = require("path");
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const TerserJSPlugin = require('terser-webpack-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const prodMode = process.env.NODE_ENV === 'production';
const distPath = path.resolve(__dirname, "./dist/assets");
// const distPath = path.resolve(__dirname, "./src-simple/dist");
//这是 react-isomorphic 的webpack配置，webpack版本 4.44.2
const minimizer = [];
if (prodMode) {
    minimizer.push(new TerserJSPlugin({}));
    minimizer.push(new OptimizeCSSAssetsPlugin({}));
}

const optimization = {
    runtimeChunk: {
        name: 'runtime',
    },
    splitChunks: {
        cacheGroups: {
            commons: {
                test: /[\\/]node_modules[\\/](antd|react-router|react-router-dom|lodash|axios|moment)[\\/]/,
                name: 'vendor',
                chunks: 'all',
            },
        },
    },
    usedExports: true,
    minimizer,
}

const obj = {
    entry: {
        client: "./src/widgets/client.js",
        // test: "./src-simple/index.js",
    },
    output: {
        filename: "[name].js",
        globalObject: "this",
        path: distPath,
        libraryTarget: "umd",
        publicPath: '/dist/assets'
    },
    mode: prodMode ? 'production' : 'development',
    devtool: "source-map",
    optimization,
    module: {
        rules: [
            // {
            //     test: /\.tsx?$/,
            //     use: 'ts-loader',
            //     exclude: /node_modules/,
            // },
            {
                test: /\.(js|ts|tsx)?$/,
                include: [path.resolve('src')],// 只在src文件夹下查找
                exclude: /node_modules/,
                loader: 'babel-loader',
                options: {
                    cacheDirectory: true
                }
            }, {
                test: /\.less|.css$/,
                use: [
                    {
                        loader: MiniCssExtractPlugin.loader,
                        options: {
                            publicPath: distPath,
                            // hmr: !prodMode,
                            // reloadAll: true,
                        },
                    },
                    // {
                    //     loader: 'style-loader', // creates style nodes from JS strings, work in browser env, if in node env, use MiniCssExtractPlugin.loader instead!
                    // },
                    {
                        loader: 'css-loader',
                        options: {
                            url: (url, resourcePath) => {
                                if (url.toLowerCase().endsWith('.png') || url.toLowerCase().endsWith('.jpg')) {
                                    return false;
                                }

                                return true;
                            },
                        }
                    },
                    {
                        loader: 'less-loader',
                        options: {
                            lessOptions: {
                                javascriptEnabled: true
                            }
                        }
                    },
                ],
            }, {
                test: /\.(png|svg|jpg|gif)$/,
                use: [
                    {
                        loader: 'url-loader',
                        options: {
                            limit: 1000,
                            outputPath: "images",
                            publicPath: '/assets/images',
                        },
                    },
                ],
            },
        ],
    },
    resolve: {
        extensions: ['.tsx', '.ts', '.js'],
    },
    plugins: [
        new MiniCssExtractPlugin({
            filename: '[name].css',
            ignoreOrder: false,
        }),
    ],
    externals: {
        //"源码里import from 的名字":"该库在window对象上暴露的名字"
        "react": "React",
        "react-dom": "ReactDOM",
    }
};

if (prodMode) {
    delete obj.devtool;
} else {
    obj.watch = true;
}

module.exports = obj;